<?php
/**
 *
 */

use App\Models\SsBasic\AreaSkuEstimate;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Arr;

    /**
     * 格式化日期
     * @param string $str
     * @param string $timeZone
     * @param string $format
     * @return false|string
     */
    function getDateFormat($str, $timeZone = 'UTC', $format = "Y-m-d\TH:i:s\Z"){
        return date_format(date_create($str, timezone_open($timeZone)), $format);
    }

    /**
     * 转化时间为毫秒时间戳
     * @param int|string $time 时间戳|日期
     * @return int
     */
    function msectime($time){
        if (is_numeric($time)) {
            return (int) str_pad($time, 13, "0", STR_PAD_RIGHT);
        } else {
            return strtotime($time) * 1000;
        }
    }

    /**
     * 时间戳转化为日期
     * @param int|string $time 时间戳|日期
     * @param string $format 格式化方式
     * @return string
     */
    function msectimeFormat($time, $format = 'Y-m-d'){
        if (is_numeric($time)) {
            return date($format, substr($time, 0, 10));
        } else {
            return $time;
        }
    }

    function xmlToArr($xml){
        libxml_disable_entity_loader(true);//禁止引用外部xml实体
        return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
    }

    /**
     * 返回执行日期所在周的第N天(默认周一)日期
     * @param     $time
     * @param int $day
     * @return false|string
     */
    function getDaysForWeek($time,$day = 1){
        $week = date('w',$time);
        if(0 == $week)$week = 7;
        $diff = $week - $day;
        return date("Y-m-d",$time - ($diff * 60 * 60 * 24));
    }

    /**
     * SimpleXMLElement Object 2 Array
     * @param $obj
     * @return array
     */
    function toArray($obj) {
        if(is_object($obj)) $obj = get_object_vars($obj);
        if(is_array($obj)){
            foreach($obj as $k=>$v){
                $obj[$k] = toArray($v);
            }
        }
        return $obj;
    }

    function editableSourceOption($data,$options = []){
        $sourceOptions = [];
        foreach ($data as $key => $value) {
            $sourceOptions[] = [
                'value' => $key,
                'text'  => $value,
            ];
        }
        return array_merge(['source'=>$sourceOptions], $options);
    }

    function editableSourceJsonOption($data){
        $sourceOptions = [];
        foreach ($data as $key => $value) {
            $sourceOptions[] = [
                'value' => $key,
                'text'  => $value,
            ];
        }
        return json_encode($sourceOptions);
    }

    function numberFormat($number, $decimals = 3, $dec_point = '.', $thousands_sep = '') {
        return number_format($number, $decimals, $dec_point, $thousands_sep);
    }

    /**
     * 获取百分比
     * @param $numerator
     * @param $denominator
     * @return string
     */
    function getPercentage($numerator,$denominator){
        return floatval($denominator) > 0 ? round($numerator / $denominator,5) * 100 .'%' : '0%';
    }

    /**
     * 多个连续空格只保留一个
     *
     * @param string $string 待转换的字符串
     * @return string $string 转换后的字符串
     */
    function mergeSpaces($string){
    //    return preg_replace("/[\s]+/is"," ",$string);
        return preg_replace("/\s(?=\s)/","\\1",$string);
    }

    function displayStatus() {
        return ['1' => '<span class="label label-info">是</span>', '0' => '<span class="label label-danger">否</span>'];
    }

    function displayApprovalStatus() {
        return [1 => '<span class="label label-danger">未审核</span>', 2 => '<span class="label label-info">已审核</span>'];
    }

    function statusSwitch() {
        return [
            'on'=>['value'=> '1','text'=>'是','color'=>'info'],
            'off'=>['value'=>'2','text'=>'否','color'=>'danger']
        ];
    }

    /**
     * Using
     * @return array
     */
    function statusOptions() {
        return [
            'on'=>['value'=> '1','text'=>'是','color'=>'info'],
            'off'=>['value'=>'0','text'=>'否','color'=>'danger']
        ];
    }

    function getOptions($options,$value = null,$flag = true){
        $str = $flag ? '<option></option>' : '';
        foreach ($options as $k => $option){
            $selected = !is_null($value) && $k == $value ? 'selected' : '';
            $str .= "<option value=\"$k\" $selected>$option</option>\\r\\n";
        }
        return new HtmlString($str);
    }

    function getMultipleOptions($options,$value = [],$flag = true){
        $str = $flag ? '<option></option>' : '';
        foreach ($options as $k => $option){
            $selected = in_array($k, $value) ?'selected':'';
            $str .= "<option value=\"$k\" $selected>$option</option>\\r\\n";
        }
        return new HtmlString($str);
    }

    function getAction($keys = []){
        $request = request();
        array_push($keys,'page');
        $query = $request->query();
        Arr::forget($query, $keys);
        $question = $request->getBaseUrl().$request->getPathInfo() == '/' ? '/?' : '?';
        return count($request->query()) > 0
            ? $request->url().$question.http_build_query($query)
            : $request->fullUrl();
    }

    function public_uploads_path($path = '')
    {
        return public_path('uploads').($path ? DIRECTORY_SEPARATOR.ltrim($path, DIRECTORY_SEPARATOR) : $path);
    }

    /**
     * 正则表达式来匹配日期格式 "YYYY-MM-DD"
     * @param $date
     * @return bool
     */
    function checkDateFormat($date)
    {
        //匹配日期格式
        if(preg_match("/^([0-9]{4})(-|/)([0-9]{2})(-|/)([0-9]{2})$/", $date, $parts)) {
            //检测是否为日期
            if(checkdate($parts[2],$parts[3],$parts[1])){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    function checkDecimal($decimal)
    {
        if(preg_match("/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/", $decimal)) {
            return true;
        }else{
            return false;
        }
    }

    function replaceSpecChar($subject) {
        $charMap = array(
            "ъ" => "-", "ь" => "-", "Ъ" => "-", "Ь" => "-",
            "А" => "A", "Ă" => "A", "Ǎ" => "A", "Ą" => "A", "À" => "A", "Ã" => "A", "Á" => "A", "Æ" => "A", "Â" => "A", "Å" => "A", "Ǻ" => "A", "Ā" => "A", "א" => "A",
            "Б" => "B", "ב" => "B", "Þ" => "B",
            "Ĉ" => "C", "Ć" => "C", "Ç" => "C", "Ц" => "C", "צ" => "C", "Ċ" => "C", "Č" => "C", "©" => "C", "ץ" => "C",
            "Д" => "D", "Ď" => "D", "Đ" => "D", "ד" => "D", "Ð" => "D",
            "È" => "E", "Ę" => "E", "É" => "E", "Ë" => "E", "Ê" => "E", "Е" => "E", "Ē" => "E", "Ė" => "E", "Ě" => "E", "Ĕ" => "E", "Є" => "E", "Ə" => "E", "ע" => "E",
            "Ф" => "F", "Ƒ" => "F",
            "Ğ" => "G", "Ġ" => "G", "Ģ" => "G", "Ĝ" => "G", "Г" => "G", "ג" => "G", "Ґ" => "G",
            "ח" => "H", "Ħ" => "H", "Х" => "H", "Ĥ" => "H", "ה" => "H",
            "I" => "I", "Ï" => "I", "Î" => "I", "Í" => "I", "Ì" => "I", "Į" => "I", "Ĭ" => "I", "I" => "I", "И" => "I", "Ĩ" => "I", "Ǐ" => "I", "י" => "I", "Ї" => "I", "Ī" => "I", "І" => "I",
            "Й" => "J", "Ĵ" => "J",
            "ĸ" => "K", "כ" => "K", "Ķ" => "K", "К" => "K", "ך" => "K",
            "Ł" => "L", "Ŀ" => "L", "Л" => "L", "Ļ" => "L", "Ĺ" => "L", "Ľ" => "L", "ל" => "L",
            "מ" => "M", "М" => "M", "ם" => "M",
            "Ñ" => "N", "Ń" => "N", "Н" => "N", "Ņ" => "N", "ן" => "N", "Ŋ" => "N", "נ" => "N", "ŉ" => "N", "Ň" => "N",
            "Ø" => "O", "Ó" => "O", "Ò" => "O", "Ô" => "O", "Õ" => "O", "О" => "O", "Ő" => "O", "Ŏ" => "O", "Ō" => "O", "Ǿ" => "O", "Ǒ" => "O", "Ơ" => "O",
            "פ" => "P", "ף" => "P", "П" => "P",
            "ק" => "Q",
            "Ŕ" => "R", "Ř" => "R", "Ŗ" => "R", "ר" => "R", "Р" => "R", "®" => "R",
            "Ş" => "S", "Ś" => "S", "Ș" => "S", "Š" => "S", "С" => "S", "Ŝ" => "S", "ס" => "S",
            "Т" => "T", "Ț" => "T", "ט" => "T", "Ŧ" => "T", "ת" => "T", "Ť" => "T", "Ţ" => "T",
            "Ù" => "U", "Û" => "U", "Ú" => "U", "Ū" => "U", "У" => "U", "Ũ" => "U", "Ư" => "U", "Ǔ" => "U", "Ų" => "U", "Ŭ" => "U", "Ů" => "U", "Ű" => "U", "Ǖ" => "U", "Ǜ" => "U", "Ǚ" => "U", "Ǘ" => "U",
            "В" => "V", "ו" => "V",
            "Ý" => "Y", "Ы" => "Y", "Ŷ" => "Y", "Ÿ" => "Y",
            "Ź" => "Z", "Ž" => "Z", "Ż" => "Z", "З" => "Z", "ז" => "Z",
            // "S" => "Z",
            "а" => "a", "ă" => "a", "ǎ" => "a", "ą" => "a", "à" => "a", "ã" => "a", "á" => "a", "æ" => "a", "â" => "a", "å" => "a", "ǻ" => "a", "ā" => "a", "א" => "a",
            "б" => "b", "ב" => "b", "þ" => "b",
            "ĉ" => "c", "ć" => "c", "ç" => "c", "ц" => "c", "צ" => "c", "ċ" => "c", "č" => "c", "©" => "c", "ץ" => "c",
            "Ч" => "ch", "ч" => "ch",
            "д" => "d", "ď" => "d", "đ" => "d", "ד" => "d", "ð" => "d",
            "è" => "e", "ę" => "e", "é" => "e", "ë" => "e", "ê" => "e", "е" => "e", "ē" => "e", "ė" => "e", "ě" => "e", "ĕ" => "e", "є" => "e", "ə" => "e", "ע" => "e",
            "ф" => "f", "ƒ" => "f",
            "ğ" => "g", "ġ" => "g", "ģ" => "g", "ĝ" => "g", "г" => "g", "ג" => "g", "ґ" => "g",
            "ח" => "h", "ħ" => "h", "х" => "h", "ĥ" => "h", "ה" => "h",
            "i" => "i", "ï" => "i", "î" => "i", "í" => "i", "ì" => "i", "į" => "i", "ĭ" => "i", "ı" => "i", "и" => "i", "ĩ" => "i", "ǐ" => "i", "י" => "i", "ї" => "i", "ī" => "i", "і" => "i",
            "й" => "j", "Й" => "j", "Ĵ" => "j", "ĵ" => "j",
            "ĸ" => "k", "כ" => "k", "ķ" => "k", "к" => "k", "ך" => "k",
            "ł" => "l", "ŀ" => "l", "л" => "l", "ļ" => "l", "ĺ" => "l", "ľ" => "l", "ל" => "l",
            "מ" => "m", "м" => "m", "ם" => "m",
            "ñ" => "n", "ń" => "n", "н" => "n", "ņ" => "n", "ן" => "n", "ŋ" => "n", "נ" => "n", "ŉ" => "n", "ň" => "n",
            "ø" => "o", "ó" => "o", "ò" => "o", "ô" => "o", "õ" => "o", "о" => "o", "ő" => "o", "ŏ" => "o", "ō" => "o", "ǿ" => "o", "ǒ" => "o", "ơ" => "o",
            "פ" => "p", "ף" => "p", "п" => "p",
            "ק" => "q",
            "ŕ" => "r", "ř" => "r", "ŗ" => "r", "ר" => "r", "р" => "r", "®" => "r",
            "ş" => "s", "ś" => "s", "ș" => "s", "š" => "s", "с" => "s", "ŝ" => "s", "ס" => "s",
            "т" => "t", "ț" => "t", "ט" => "t", "ŧ" => "t", "ת" => "t", "ť" => "t", "ţ" => "t",
            "ù" => "u", "û" => "u", "ú" => "u", "ū" => "u", "у" => "u", "ũ" => "u", "ư" => "u", "ǔ" => "u", "ų" => "u", "ŭ" => "u", "ů" => "u", "ű" => "u", "ǖ" => "u", "ǜ" => "u", "ǚ" => "u", "ǘ" => "u",
            "в" => "v", "ו" => "v",
            "ý" => "y", "ы" => "y", "ŷ" => "y", "ÿ" => "y",
            "ź" => "z", "ž" => "z", "ż" => "z", "з" => "z", "ז" => "z", "ſ" => "z",
            "™" => "tm",
            "@" => "at",
            "Ä" => "ae", "Ǽ" => "ae", "ä" => "ae", "æ" => "ae", "ǽ" => "ae",
            "ĳ" => "ij", "Ĳ" => "ij",
            "я" => "ja", "Я" => "ja",
            "Э" => "je", "э" => "je",
            "ё" => "jo", "Ё" => "jo",
            "ю" => "ju", "Ю" => "ju",
            "œ" => "oe", "Œ" => "oe", "ö" => "oe", "Ö" => "oe",
            "щ" => "sch", "Щ" => "sch",
            "ш" => "sh", "Ш" => "sh",
            "ß" => "ss",
            "Ü" => "ue",
            "Ж" => "zh", "ж" => "zh",
        );
        return strtr($subject, $charMap);
    }

    function replaceAmountChar($subject) {
        $match = ['??','鈧','ｿ･','拢','Can','can','C','c','$','＄','¥','￥','£','￡','€','￠',',',' '];
        return floatval(str_replace($match,'',$subject));
    }

    function matchSkuByCampaign($str){
        $pos = strripos($str,'AM0');
        if(!$pos)return '';
        return strtoupper(substr($str,$pos,7));
    }

    /**
     * 取时间
     * @return string
     */
    function getMicroTime(){
        list($u_sec,$sec) = explode(" ",microtime());
        $num = ((float)$u_sec+(float)$sec);
        return sprintf("%.4f",$num);
    }

    /**
     * 取内存占用情况
     * @return string
     */
    function getMemoryUsage() {
        return (!function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB';
    }


    //运行情况，开始
    function startRunOS(){
        //[运行开始时间, 内存占用大小]
        return [time(), getMemoryUsage()];
    }

    //运行情况，结束
    function endRunOS($method, $stratTime, $stratStorage){

        $timeNumber = time() - $stratTime ?? 0;  //运行时间 = （运行结束时间 - 运行开始时间）;

        //[运行时间（秒）, 内存占用]
        $message = '【'.$method.'】耗时情况：'.$timeNumber.'秒, 内存使用：运行前：'.$stratStorage.', 运行后：'. getMemoryUsage();

        return $message ?? '';
    }

    /**
     * @param $dateInt1
     * @param $dateInt2
     * @return float|int
     */
    function getMonthDiffByDateInt($dateInt1,$dateInt2){
        if($dateInt1 > $dateInt2){
            $tmp = $dateInt2;
            $dateInt2 = $dateInt1;
            $dateInt1 = $tmp;
        }
        list($y1,$m1) = explode("-",date('Y-m',$dateInt1));
        list($y2,$m2) = explode("-",date('Y-m',$dateInt2));
        return abs($y1 - $y2) * 12 + $m2 - $m1;
    }

    /**
     * @param $smallDateStr
     * @param $bigDateStr
     * @return float|int
     */
    function getMonthDiffByDateStr($smallDateStr,$bigDateStr){
        list($y1,$m1) = explode("-",$smallDateStr);
        list($y2,$m2) = explode("-",$bigDateStr);
        return abs($y1 - $y2) * 12 + $m2 - $m1;
    }

    function getDayDiffByDateStr($smallDateStr,$bigDateStr){
        $start = new DateTime($smallDateStr);
        $end = new DateTime($bigDateStr);
        return $end->diff($start)->days;
    }

    /**
     * @param array $con
     * @return array
     */
    function formatCondition($con = [])
    {
        $conArr = [];
        foreach ($con as $field => $val) {
            $conArr[] = [$field,$val];
        }
        return $conArr;
    }

    /**
     * 格式处理,实际发货订单号批次
     * @param $strNo
     * @param string $separate
     * @param int $number
     * @return string
     */
    function formatStrOrderNo($strNo, $separate = '-', $number = 1){

        //正例: 123456-654321-1
        //正例: 123456-654321-2
        //正例: 123456-654321-3

        $deliveryOrderNo = $strNo;
        if ($deliveryOrderNo) {
            if (strpos($strNo, $separate)) {  //验证字符串中是否存在'-'(分隔符号)
                $strSubVal = substr($strNo, -2, 1); //验证字符串中倒数第2位是否为'-'
                if ($strSubVal == $separate) {
                    $strPrevVal = substr($strNo, 0, -1); //'-'前值
                    $strLastVal = substr($strNo, -1); //'-'后值
                    $batchNum = abs($strLastVal) + $number; //累加批次号
                    $deliveryOrderNo = $strPrevVal . $batchNum; //订单尾数累加数量(批次)
                }else{
                    $deliveryOrderNo = $strNo . $separate . $number; //订单尾数赋初始值(批次)
                }
            }else{
                $deliveryOrderNo = $strNo . $separate . $number; //订单尾数赋初始值(批次)
            }
        }

        return $deliveryOrderNo;
    }

    /**
     * 格式化邮编(前三后四)
     * @param $str
     * @param string $separate
     * @param int $prevNumber
     * @param int $lastNumber
     * @return string
     */
    function formatPostcode($str, $separate = '-', $prevNumber = 3, $lastNumber = 4){
        //$str(5080003)
        $postcode = '';
        if ($str) {
            if (strpos($str, $separate)) {  //验证字符串中是否存在'-'(分隔符号)
                $postcode = $str;
            }else{
                $strPrevVal = substr($str, 0, $prevNumber); //邮编前三位(508)
                $strLastVal = substr($str, -$lastNumber);   //邮编后四位(0003)
                $postcode = $strPrevVal .$separate. $strLastVal;
            }
        }

        return $postcode; //邮编(508-0003)
    }

    /**
     * 去除字符中的全部空格(去除所有空格)
     * @param $str_search
     * @param $replace
     * @param $str
     * @return string|string[]
     */
    function formatTrim($str_search, $replace, $str){

        //str_replace(替换的内容, 替换的格式, 字符串);
        $res = str_replace($str_search, $replace, $str);

        return $res ?? '';
    }


    /**
     * 计算两个日期之间相差的天数 (24小时格式：2022-01-01 00:00:00)
     * @param $date1
     * @param $date2
     * @return int
     */
    function formatDiffDateDay($date1, $date2){

        if ($date1 > $date2) {
            $dateMax = strtotime($date1);
            $dateMin = strtotime($date2);
        } else {
            $dateMax = strtotime($date2);
            $dateMin = strtotime($date1);
        }
        $days = ($dateMax - $dateMin) / 86400; //天数

        return intval($days) ?? 0;
    }

    /**
     * 计算两个日期之间相差的天数 (时间戳格式：1640966400)
     * @param $time1
     * @param $time2
     * @return int
     */
    function formatDiffTimeDay($time1, $time2){

        if ($time1 > $time2) {
            $timeMax = $time1;
            $timeMin = $time2;
        }else{
            $timeMax = $time2;
            $timeMin = $time1;
        }
        $days = ($timeMax - $timeMin) / 86400; //天数

        return intval($days) ?? 0;
    }

    /**
     * 获取集合平均值 (依据条件计算平均值)
     * @param $dayArray
     * @param int $day
     * @return array
     */
    function getSalesReportDailyAvg($dayArray, $day = 30){

        $avgArr = [];
        $sum3 = 0;
        $sum7 = 0;
        $sum15 = 0;
        $sum30 = 0;

        for($i = $day; $i >= 1; $i--){

            $dayTitle = date("Y-m-d", strtotime("-$i day"));

            $number = 0;
            if(array_key_exists($dayTitle, $dayArray)){
                $number = $dayArray[$dayTitle]; //当期数值(当天)
            }
            if($i <= 3){
                $sum3 += $number;
            }
            if($i <= 7){
                $sum7 += $number;
            }
            if($i <= 15){
                $sum15 += $number;
            }

            $sum30 += $number;
        }

        $avgArr['avg3'] = round($sum3 / 3, 3); //近3天平均值
        $avgArr['avg7'] = round($sum7 / 7, 3); //近7天平均值
        $avgArr['avg15'] = round($sum15 / 15, 3); //近15天平均值
        $avgArr['avg30'] = round($sum30 / 30, 3); //近30天平均值

        return $avgArr;
    }

    /**
     * 提货单批次号
     * @param $classify
     * @param $batchNum
     * @return string
     */
    /**
     * 提货单号
     * @param int $classify
     * @param string $separate
     * @param string $batchNum
     * @return string
     */
    function getPickupOrderNo($classify = 0, $separate = '-', $batchNum = '01'){
        return 'TH'.date('Ymd',time()).str_pad($classify, 6, 0, STR_PAD_LEFT).$separate.$batchNum;
    }

    /**
     * 备货申请单号
     * @param int $classify
     * @return string
     */
    function getStockupApplyNo($classify = 0){
        return 'SAN'.date('Ymd',time()).str_pad($classify, 6, 0, STR_PAD_LEFT);
    }


    /**
     * KOL红人编码
     * @return string
     */
    function getKolNo($kolNumber = 1){
        return 'UNG'.str_pad($kolNumber, 6, 0, STR_PAD_LEFT);
    }

    /**
     * KOL红人订单号
     * @return string
     */
    function getKolOrderNo($orderNoNumber = 1){
        return 'KOL'.str_pad($orderNoNumber, 6, 0, STR_PAD_LEFT);
    }

    /**
     * 校检数据（唯一,不重复）
     * @param array $dataArray
     * @param array $keyColumn
     * @return array[]
     */
    function checkRepeatData($dataArray = [], $keyColumn = []){

        $resultData = []; //去重数据
        $errorsData = []; //异常数据
        $line = 0; //行数

        if ($dataArray) {
            foreach ($dataArray as $index => $item) {
                $line++;

                $uniqueKey = '';
                foreach ($keyColumn as $title) {
                    $value = $item[$title] ?? ''; //匹配指定键字段($title), 获取键值拼接
                    $uniqueKey = empty($uniqueKey) ? $value : $uniqueKey .'_'. $value; //例：key1_key2_key3
                }
                if (empty($uniqueKey)) {
                    $errorsData[$line] = 'key不存在';
                    continue;
                }

                //验证唯一键值是否存在
                if(!in_array($uniqueKey, $resultData)){
                    $resultData[$line] = $uniqueKey;
                }else{
                    $errorsData[$line] = '重复记录'; //例：[4] => 重复记录
                    continue;
                }
            }
        }

        return [$dataArray, $resultData, $errorsData];
    }

    /**
     * 验证开发环境
     * @return bool
     */
    function is_local_env(){
        if ((substr($_SERVER['REMOTE_ADDR'],0,8) == "192.168.") || ($_SERVER['REMOTE_ADDR'] == "127.0.0.1")) {
            return true;
        } else {
            return false;
        }
    }


    function getDateParam($startDateParam, $endDateParam){

        $startDate = date('Y-m-d',strtotime('-2 days')); //开始时间(初始值)
        $endDate = $startDate; //截止时间(初始值)

        if ($startDateParam != 'null') {
            if (!strtotime($startDateParam) || strtotime($startDateParam) > strtotime($startDate)) {
                return ['status' => false, 'message' => 'startDate Exception!'];
            }
            $startDate = $startDateParam; //赋值,自定义开始时间
        }

        if ($endDateParam != 'null') {
            if (!strtotime($endDateParam) || strtotime($endDateParam) > strtotime($endDate) || strtotime($endDateParam) < strtotime($startDate)) {
                return ['status' => false, 'message' => 'endDate Exception!'];
            }
            $endDate = $endDateParam; //赋值,自定义结束时间
        }

        return ['status' => true, 'date' => ['startDate'=> $startDate, 'endDate'=> $endDate]];
    }


    /**
     * 获取预估汇总（sql字符串）
     * @return string
     */
    function getQueryEstimateStr(){
        //拼接SQL查询字段
        $queryStr = '';
        for($i = 0; $i < AreaSkuEstimate::ESTIMATE_NUM; $i++){
            if ($i < 1) {
                $queryStr .= 'sum(estimate_num'.$i.') as estimate_num'.$i;
            }else{
                $queryStr .= ',sum(estimate_num'.$i.') as estimate_num'.$i;
            }
        }
        return $queryStr;
    }

    /**
     * 二维数组中相同值的汇总数量（相同sku的总申请数量）
     *
     * 参数：
     *"UK-AM04155-AMUK-FBA22" => array:4 [▼
     *   "psku_id" => 1789
     *   "apply_num" => "10"
     * ]
     *"UK-AM04155-AMUK-FBA66" => array:4 [▼
     *   "psku_id" => 1789
     *   "apply_num" => "20"
     * ]
     * "UK-AM04227-AMUK-FBA66" => array:4 [▼
     *   "psku_id" => 1860
     *   "apply_num" => "10"
     * ]
     *
     * 汇总结果：
     * array:2 [▼
     *      1789 => 30
     *      1860 => 10
     * ]
     * @param $itemsArray
     * @param string $keyStr
     * @param string $valueStr
     * @return array
     */
    function getArrayValueSum($itemsArray, $keyStr = 'psku_id', $valueStr = 'apply_num'){
        $result = [];
        foreach (array_unique(array_column($itemsArray, $keyStr)) as $name) {
            $result[$name] = array_sum(array_map(function ($element) use ($name,$keyStr,$valueStr) {
                return ($element[$keyStr] === $name) ? $element[$valueStr] : 0;
            }, $itemsArray));
        }
        return $result;
    }
